home *** CD-ROM | disk | FTP | other *** search
/ Komputer for Alle 2004 #2 / K-CD-2-2004.ISO / OpenOffice Sv / f_0397 / python-core-2.2.2 / lib / test / test_unicodedata.py < prev    next >
Encoding:
Python Source  |  2003-07-18  |  3.8 KB  |  126 lines

  1. """ Test script for the unicodedata module.
  2.  
  3.     Written by Marc-Andre Lemburg (mal@lemburg.com).
  4.  
  5.     (c) Copyright CNRI, All Rights Reserved. NO WARRANTY.
  6.  
  7. """#"
  8. from test_support import verify, verbose
  9. import sha
  10.  
  11. encoding = 'utf-8'
  12.  
  13. def test_methods():
  14.  
  15.     h = sha.sha()
  16.     for i in range(65536):
  17.         char = unichr(i)
  18.         data = [
  19.  
  20.             # Predicates (single char)
  21.             char.isalnum() and u'1' or u'0',
  22.             char.isalpha() and u'1' or u'0',
  23.             char.isdecimal() and u'1' or u'0',
  24.             char.isdigit() and u'1' or u'0',
  25.             char.islower() and u'1' or u'0',
  26.             char.isnumeric() and u'1' or u'0',
  27.             char.isspace() and u'1' or u'0',
  28.             char.istitle() and u'1' or u'0',
  29.             char.isupper() and u'1' or u'0',
  30.  
  31.             # Predicates (multiple chars)
  32.             (char + u'abc').isalnum() and u'1' or u'0',
  33.             (char + u'abc').isalpha() and u'1' or u'0',
  34.             (char + u'123').isdecimal() and u'1' or u'0',
  35.             (char + u'123').isdigit() and u'1' or u'0',
  36.             (char + u'abc').islower() and u'1' or u'0',
  37.             (char + u'123').isnumeric() and u'1' or u'0',
  38.             (char + u' \t').isspace() and u'1' or u'0',
  39.             (char + u'abc').istitle() and u'1' or u'0',
  40.             (char + u'ABC').isupper() and u'1' or u'0',
  41.  
  42.             # Mappings (single char)
  43.             char.lower(),
  44.             char.upper(),
  45.             char.title(),
  46.  
  47.             # Mappings (multiple chars)
  48.             (char + u'abc').lower(),
  49.             (char + u'ABC').upper(),
  50.             (char + u'abc').title(),
  51.             (char + u'ABC').title(),
  52.  
  53.             ]
  54.         h.update(u''.join(data).encode(encoding))
  55.     return h.hexdigest()
  56.  
  57. def test_unicodedata():
  58.  
  59.     h = sha.sha()
  60.     for i in range(65536):
  61.         char = unichr(i)
  62.         data = [
  63.             # Properties
  64.             str(unicodedata.digit(char, -1)),
  65.             str(unicodedata.numeric(char, -1)),
  66.             str(unicodedata.decimal(char, -1)),
  67.             unicodedata.category(char),
  68.             unicodedata.bidirectional(char),
  69.             unicodedata.decomposition(char),
  70.             str(unicodedata.mirrored(char)),
  71.             str(unicodedata.combining(char)),
  72.             ]
  73.         h.update(''.join(data))
  74.     return h.hexdigest()
  75.  
  76. ### Run tests
  77.  
  78. print 'Testing Unicode Database...'
  79. print 'Methods:',
  80. print test_methods()
  81.  
  82. # In case unicodedata is not available, this will raise an ImportError,
  83. # but still test the above cases...
  84. import unicodedata
  85. print 'Functions:',
  86. print test_unicodedata()
  87.  
  88. # Some additional checks of the API:
  89. print 'API:',
  90.  
  91. verify(unicodedata.digit(u'A',None) is None)
  92. verify(unicodedata.digit(u'9') == 9)
  93. verify(unicodedata.digit(u'\u215b',None) is None)
  94. verify(unicodedata.digit(u'\u2468') == 9)
  95.  
  96. verify(unicodedata.numeric(u'A',None) is None)
  97. verify(unicodedata.numeric(u'9') == 9)
  98. verify(unicodedata.numeric(u'\u215b') == 0.125)
  99. verify(unicodedata.numeric(u'\u2468') == 9.0)
  100.  
  101. verify(unicodedata.decimal(u'A',None) is None)
  102. verify(unicodedata.decimal(u'9') == 9)
  103. verify(unicodedata.decimal(u'\u215b',None) is None)
  104. verify(unicodedata.decimal(u'\u2468',None) is None)
  105.  
  106. verify(unicodedata.category(u'\uFFFE') == 'Cn')
  107. verify(unicodedata.category(u'a') == 'Ll')
  108. verify(unicodedata.category(u'A') == 'Lu')
  109.  
  110. verify(unicodedata.bidirectional(u'\uFFFE') == '')
  111. verify(unicodedata.bidirectional(u' ') == 'WS')
  112. verify(unicodedata.bidirectional(u'A') == 'L')
  113.  
  114. verify(unicodedata.decomposition(u'\uFFFE') == '')
  115. verify(unicodedata.decomposition(u'\u00bc') == '<fraction> 0031 2044 0034')
  116.  
  117. verify(unicodedata.mirrored(u'\uFFFE') == 0)
  118. verify(unicodedata.mirrored(u'a') == 0)
  119. verify(unicodedata.mirrored(u'\u2201') == 1)
  120.  
  121. verify(unicodedata.combining(u'\uFFFE') == 0)
  122. verify(unicodedata.combining(u'a') == 0)
  123. verify(unicodedata.combining(u'\u20e1') == 230)
  124.  
  125. print 'ok'
  126.